<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Config;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class ConfigController extends Controller
{

    public function changeContent()
    {
        $input = Input::except('_token');
        foreach($input['id'] as $k=>$v){
            //            获取此条要被替换成的内容
            Config::where('id',$v)->update(['conf_content'=>$input['conf_content'][$k]]);
        }
        $this->putFile();
        return redirect('admin/config');

    }


    public function putFile()
    {
//        将配置文件中的内容写入config目录下的web.php文件   方便后期读取网站配置
        $config = Config::pluck('conf_content','conf_name')->all();
        $str = '<?php return '.var_export($config,true).';';
        //要写入的路径
        $path = base_path().'\config\web.php';
//        参数1 写入的文件的路径  参数2  向文件中写入的内容
        file_put_contents($path,$str);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//       $data =  Config::orderBy('conf_order','asc')->get();
         $data =  Config::get();


       return view('admin.config.index',compact('data'));
    }

    //改变分类排序
    public function changeOrder()
    {
        $input = Input::except('_token');
//        dd($input);
        $conf = Config::find($input['conf_id']);
        $conf->conf_order = $input['conf_order'];
        $re = $conf->save();
        if($re){
            $data =[
                'status'=>0,
                'msg'=>'排序成功'
            ];
        }else{
            $data =[
                'status'=>1,
                'msg'=>'排序失败'
            ];
        }
        return $data;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.config.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
//        dd($request->all());

        $input = Input::except('_token');
        $input['created_at'] = date('Y-m-d h:i:s',time());
        $re = Config::create($input);
        if($re){
            return redirect('admin/config');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data =  Config::where('id',$id)->first();
        //可以使用compact函数给模板传变量
        return view('admin.config.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = Input::except(['_token','_method']);
        $re = Config::where('id',$id)->update($input);

        if($re){
            return redirect('admin/config');
        }else{
            return back()->with('errors','网站配置修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $re =   Config::where('id',$id)->delete();
        if($re){
            $this->putfile();
            $data = [
                'status'=>1,
                'msg'=>'网站配置删除成功'
            ];
        }else{
            $data = [
                'status'=>0,
                'msg'=>'网站配置删除失败'
            ];
        }
        return $data;
    }
}
